<?php

class GalleryController extends BaseController
{	   
    public function init() {
        parent::init();
        setLanguage(isset($_GET['lang']) ? $_GET['lang'] : null);
    }
    
    public function actionIndex()
    {
        $arrAlbum = Album::model()->getAllWithSortByOrderNumber(Yii::app()->session['lang']);
        $this->render('index',array('arrAlbum'=>$arrAlbum));
    }
    
    /**
     * 
     * @param int $id Album 
     */
    public function actionPhoto($id)
    {
	if (isset($_GET['date'])) {
	    $date = date_create_from_format('m/d/Y', $_GET['date']);
	    $timeSpan = $date->getTimestamp();
	    $month = date('m', $timeSpan);
	    $year = date ('Y', $timeSpan);
	    $dataProvider = Photo::model()->getPhotoByAlbumIdAndMonth($id, $month, $year, Yii::app()->session['lang']);
	} else {
	    $album = Album::model()->findByPk($id, 'status = :status', array(':status' => LGConstant::PUBLISH_STATUS));
	    
	    if ($album == NULL) {
		throw new CHttpException(404, t(404, 'messages'));
	    }
	    
	    $dataProvider = Photo::model()->getPhotoByAlbumId($id,Yii::app()->session['lang']);
	}
	
        
        $this->render('photo',array('dataProvider'=>$dataProvider,'album'=> Album::model()->findByPk($id), 'alias' => isset($_GET['alias']) ? $_GET['alias'] : ""));
    }
    
    public function actionThumbnails()
    {    
        $fileName = $_GET['f'];
        $width = $_GET['w'];
        $height = $_GET['h'];
	
	$imageInfo =  getimagesize ($fileName);
	
	if (!$imageInfo) {
	    throw new CHttpException(404, t(404, 'messages'));
	}
	
	$imageWidth = $imageInfo[0];
	$imageHeight = $imageInfo[1];
	
	
	$isWidthGreaterHeight = $imageWidth > $imageHeight;
	
	$scale = 1;
	//if ($isWidthGreaterHeight) {
	    if ($imageWidth > $width) {
		$scale = $width / ($imageWidth * 1.0);
	    }
	//} else {
	   // if ($imageHeight > $height) {
	//	$scale = $height / ($imageHeight * 1.0);
	  //  }
	//}
	
        $image = new ResizeImage($fileName, $imageWidth * $scale,$imageHeight * $scale);                         
        echo $image->crop(0, 0, $width, $height);
    }
  
}
